angular.module('FirstConfirm')
    .controller('FirstConfirm.FirstConfirmCtrl', ['$scope', '$state', '$http', '$ionicLoading', '$ionicPopup', '$timeout', 'Http', 'imanDialog', 'DevicePort', 'User', 'Device',
        function ($scope, $state, $http, $ionicLoading, $ionicPopup, $timeout, Http, imanDialog, DevicePort, User, Device) {
            $scope.data = {
                isCheck: false,
                getQc: false,
                Barcode: '',
                localConfig: {},
                User: User.get(),
                device: Device.get(),
                select: '',
                workId: ''
            };


            $scope.$on('$ionicView.afterEnter', function () {
                DevicePort.open({
                    device: 0,
                    baudrate: 115200,
                    parseFn: true
                }).then(function () {
                    //imanDialog.toastBottom("成功");
                }, function () {
                    imanDialog.toastBottom("端口打开失败");
                });
            });

            $scope.$on('devicePort.DataReceived', function (e, barcode) {
                $scope.data.Barcode = barcode;
                $scope.data.isCheck = false;
                $scope.data.getQc = false;
                $scope.data.select = '';
                getQcInfo(barcode);
            });
            $scope.submit = function () {
                $ionicLoading.show();
                Http.post({
                    url: '/api/FirstQC/setFirstQCInfo',
                    data: {
                        Parameter: {
                            User: $scope.data.User,
                            Terminal: $scope.data.device.Terminal,
                            WorkLocation: $scope.data.device.WorkLocation,
                            Work: $scope.data.workId,
                            // Work: $scope.data.device.Work,
                            BarcodeMaster: $scope.data.BarcodeMaster,
                            BizRecord: $scope.data.BizRecord,
                            QCRecords: $scope.data.QCRecords
                        }
                    }
                }).then(function () {
                    $ionicLoading.hide();
                    $scope.data.getQc = true;
                    imanDialog.toastCenter('确认成功');
                    $timeout(function () {
                        imanDialog.toastTop('请扫描单品信息');
                    }, 2000);
                }, function (err) {
                    $ionicLoading.hide();
                })['finally'](function () {
                    $ionicLoading.hide();
                });
            };

            function getQcInfo(Barcode) {
                $ionicLoading.show();
                if (!$scope.data.device.Work) {
                    imanDialog.toastBottom('没有获取到作业信息');
                    return;
                }
                Http.post({
                    url: '/api/FirstQC/getFirstQCDefaultInfo',
                    data: {
                        Parameter: {
                            Barcode: Barcode,
                            //WorkID: $scope.data.device.Work._id,
                            firstFlag: true
                        }
                    }
                }).then(function (infoData) {
                    $scope.data.workId = infoData.workId;
                    operateBarCode(infoData);
                })['finally'](function () {
                    $ionicLoading.hide();
                });
            }

            $scope.$on('$ionicView.beforeLeave', function () {
                DevicePort.close();
            });

            function operateBarCode(info) {
                $scope.data.MO = info.barcodeData.BarcodeMaster.MO;
                $scope.data.BarcodeMaster = info.barcodeData.BarcodeMaster;
                $scope.data.BizRecord = info.barcodeData.BizRecord;
                $scope.data.QCRecords = info.barcodeData.QCRecords;
                $scope.data.QCNorm = info.barcodeData.QCNorm;
                $scope.data.Work = info.workId;
                $scope.data.Remark = info.barcodeData.Remark;
                if ($scope.data.BizRecord && $scope.data.BizRecord.QCUser) {
                    //测试使用todo
                    $scope.data.isCheck = true;
                }
                angular.forEach($scope.data.QCRecords, function (value) {
                    switch (value.QCNorm.Type) {
                        case 0:
                            value.NormValue = Number(value.NormValue);
                            break;
                        case 2:
                            switch (value.NormValue) {
                                case 'false':
                                    value.NormValue = false;
                                    break;
                                case 'true':
                                    value.NormValue = true;
                                    break;
                            }
                            break;
                    }
                });
            }

            $scope.showSelectDialog = function (obj) {
                Http.post({
                    url: '/api/Judge/getExtendEnum',
                    data: {
                        Parameter: {
                            Code: 'SQC04'
                        }
                    }
                }).then(function (info) {
                    var SQC = [];
                    angular.forEach(info, function (value, key) {
                        SQC.push({id: key, name: value});
                        if (obj.NormValue === value) {
                            $scope.data.ListId = key;
                        }
                    });
                    imanDialog.showList({
                        title: '请选择',
                        list: SQC,
                        valueField: 'id',
                        displayField: 'name',
                        value: $scope.data.ListId ? $scope.data.ListId : 0
                    }).then(function (select) {
                        obj.NormValue = select.name;
                        $scope.data.ListId = select.id;
                    });
                });
            };
        }
    ]);
